package com.u.finance.loans.mq;

import com.u.finance.loans.entity.Loans;
import com.u.finance.loans.service.ILoansService;
import jakarta.annotation.Resource;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @program: u-finance
 * @description: 申请审批的消费类
 * @author: Alex Wu
 * @createDate: 2025-03-28 17:03
 **/
@RocketMQMessageListener(topic = "tx-loans-topic",consumerGroup = "tx-loans-topic-group")
public class ApplyLoansConsumer {

    @Autowired
    private ILoansService loansService;

    @Resource
    private RocketMQTemplate rocketMQTemplate;

    //TODO 1.接收消息
    //TODO 2.调用审批服务
    //TODO 3.发送审批结果
    public void onMessage(Loans loans){
        System.out.println("AI判断是否能够直接审批:"+ loans + ",可以的话则直接发送审批结果!否者就到人工审批");
        Integer result = fetchAIApprove(loans);
        Loans approvedLoans = loansService.getById(loans.getId());

        if(result == 0){
            System.out.println("AI审批不通过,需要人工审批!");
            approvedLoans.setApproveStatus(2);
        }else{
            System.out.println("AI审批通过,发送审批结果!");
        }
        loansService.create(loans);

        rocketMQTemplate.convertAndSend("payout-topic",approvedLoans);
    }

    private Integer fetchAIApprove(Loans loans){
        return 1;
    }
}
